package com.example.smartcity.ui.Tools;

import android.os.Bundle;

import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.smartcity.R;

/**
 * Created by Caleb.F
 * on 2020-12-03
 * at 23:46
 */
public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());

    }


    protected abstract int getLayout();

    protected void initToolBar(String toolBarTitle,Boolean isShowHomeAsUp) {
        Toolbar toolbar = findViewById(R.id.toolbar_main);
        toolbar.setVisibility(View.VISIBLE);
        TextView toolbar_text = findViewById(R.id.toolbar_text);
        toolbar.setTitle("");
        toolbar_text.setText(toolBarTitle);
        setSupportActionBar(toolbar);
        if (getSupportActionBar()!=null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(isShowHomeAsUp);
        }

    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    protected void hideToolBar(){
        Toolbar toolbar = findViewById(R.id.toolbar_main);
        toolbar.setVisibility(View.GONE);
    }

    /**
     * 新增处理fragment的返回栈问题
     */
    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount()!=0) {
            getSupportFragmentManager().popBackStack();
        }
        super.onBackPressed();
    }
}
